package com.powernode.controller;

import cn.hutool.core.date.DateUtil;
import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.Credentials;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentials;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.PutObjectRequest;
import com.powernode.config.AliyunOSSConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * 文件上传控制层
 * @Author: DongGuoZhen
 * @Date: 2025/10/09/15:12
 * @Description:
 */
@Api(tags = "文件上传接口管理")
@RequestMapping("admin/file")
@RestController
public class FileUploadController {


    @Autowired
    private AliyunOSSConfig aliyunOSSConfig;


    /**
     * 上传单个文件接口
     * 1. 请求方法必须为post
     * 2. 接收文件的参数必须为MultipartFile -该对象是springmvc提供的
     * @return
     */
    @ApiOperation("上传单个文件接口")
    @PostMapping("upload/element")
    public String uploadFile(MultipartFile file) {
//        以天为单位，作为文件夹名称
        String newFolderName = DateUtil.format(new Date(), "yyyy-MM-dd");
//        以时间戳作为文件的新名称
        String newFileName = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
//        获取源文件后缀名称
        String originalFilename = file.getOriginalFilename();
        String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String objectName = newFolderName + "/" + newFileName + fileSuffix;
        CredentialsProvider provider = new CredentialsProvider() {
            @Override
            public void setCredentials(Credentials credentials) {}
            @Override
            public Credentials getCredentials() {
                return new DefaultCredentials(aliyunOSSConfig.getAccessKeyId(), aliyunOSSConfig.getAccessKeySecret());
            }
        };
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(aliyunOSSConfig.getEndpoint())
                .credentialsProvider(provider)
                .clientConfiguration(clientBuilderConfiguration)
                .region(aliyunOSSConfig.getRegion())
                .build();
       String url = "";
        try {
            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(aliyunOSSConfig.getBucketName(), objectName, file.getInputStream());
            // 上传文件。
            ossClient.putObject(putObjectRequest);
            url = aliyunOSSConfig.getAliDomalt() + objectName;
        }  catch (Exception ce) {
            System.out.println("Caught an ClientException, which means the client encountered ");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return url.toString();
    }
}
